<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$type=empty($_GPC['type']) ? 'all' :$_GPC['type'];
$state=$_GPC['state'];
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$where="WHERE a.uniacid=:uniacid ";
$data[':uniacid']=$_W['uniacid'];
$test = pdo_get('snx_unit',array('id'=>$_GPC['id']),array('id'));
$unit_id=$_GPC['id'];
if(isset($_GPC['keywords'])){
    $where.="  and a.title LIKE  concat('%', :name,'%')";
    $data[':name']=$_GPC['keywords']; 
    $type='all';
}
if (isset($_GPC['id'])) {
    
$where.= " and a.unit_id=".$unit_id;
}else{
    $where.= " and a.unit_id=".$info['unit_id'];
}
   
$sql="SELECT a.*,b.unit_name,b.num,c.name FROM ".tablename('snx_task'). " a"  . " left join " . tablename("snx_unit") . " b on a.unit_id=b.id " . " left join " . tablename("snx_course") . " c on b.cour_id=c.id " .$where." ORDER BY a.create_time desc";

$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('snx_task'). " a"  . " left join " . tablename("snx_unit") . " b on a.unit_id=b.id " . " left join " . tablename("snx_course") . " c on b.cour_id=c.id " .$where." ORDER BY a.create_time desc",$data);
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
// var_dump($list);die;
$pager = pagination($total, $pageindex, $pagesize);

if($_GPC['op']=='delete'){
    // $order = pdo_get('snx_task',array('id'=>$_GPC['id']));
//    $tasks = pdo_getall('wxc_task',array('order_id'=>$order['id']));
//    print_r(json_encode($tasks));die;
    $res=pdo_delete('snx_task',array('id'=>$_GPC['id']));

    if($res){
        message('删除成功！', $this->createWebUrl('task',array('id'=>$unit_id)), 'success');
    }else{
        message('删除失败！','','error');
    }
}


//  if($type=='published'){
//     $where.=" and b.state=1";
// }
// if($type=='finished'){
//     $where.=" and b.state=2";
// }


//print_r(json_encode($list));die;
// var_dump($list);die;
// foreach ($list as $b){
//     $end_time = strtotime($b['end_time']);
//     if ($end_time < time() && $b['state']==1){
//         $res=pdo_update('snx_task',array('state'=>2),array('id'=>$b['id']));
//     }if ($_GPC['op'] == 'change') {
//           $res=pdo_update('snx_task',array('status'=>$_GPC['status']),array('id'=>$_GPC['id']));
//           if($res){
//               message('操作成功',$this->createWebUrl('task',array()),'success');
//           }else{
//               message('操作失败','','error');
//           }
//       }
// }

include $this->template('web/task');